package com.huixue.cn.ztej.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.huixue.cn.ztej.base.Constant;
import com.huixue.cn.ztej.bean.Item;
import com.huixue.cn.ztej.bean.Project;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.Section;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.constants.UserUtil;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.SysProjectService;
import com.huixue.cn.ztej.service.SysSectionService;
import com.huixue.cn.ztej.service.SysUserService;
import com.huixue.cn.ztej.utils.MD5;
import com.huixue.cn.ztej.utils.Urls;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.weixin.bean.OAuthInfo;
import com.huixue.cn.ztej.weixin.util.WeixinHelper;
import com.huixue.cn.ztej.weixin.util.WeixinUtil;

@Controller
@RequestMapping(value = "/user")
public class WUserController  extends BaseController {

	@Autowired(required = true)
	SysUserService sysUserService;
	
	@Autowired(required = true)
	SysProjectService sysProjectService;
	
	@Autowired(required = true)
	SysSectionService sysSectionService;
	
	@RequestMapping(value = "/reg")
	public String registerBefore(HttpServletRequest request,HttpServletResponse response,Model model) {
		try{
			OAuthInfo aouAuthInfo  = WeixinHelper.getUserOpenId(request);
			User currentUser = sysUserService.findUserByOpenId(aouAuthInfo.getOpenId(),false);
			model.addAttribute("openID",aouAuthInfo.getOpenId());
			Map<String, Object> searchParams = new HashMap<String,Object>();
			Results results = sysProjectService.searchDtosByPage(searchParams,1,1000);
			
			model.addAttribute("res",results);
						
			List<Item> items = new ArrayList<Item>();
			if(results.getData()!=null){
				List<Project> list = (List<Project>)((Map)results.getData()).get("rows");
				if(list!=null && list.size()>0){
					for(int i = 0;i<list.size();i++){
						Project pro = list.get(i);
						Item item = new Item(pro.getProId()+"",pro.getName());
						items.add(item);
					}
				}
			}
			model.addAttribute("projects",new JacksonUtil().getJson(items));
			if(currentUser==null){
				return "/wx/user/register";
			}else if(currentUser.getStatus()==UserUtil.APPROVE_NO){
				model.addAttribute("msg", "注册成功,等待审批");
				return "/wx/user/approve";
			}else if(currentUser.getIsAvailable()==UserUtil.DISABLE_YES){
				model.addAttribute("msg", "账号已禁用,请联系管理员");
				return "/wx/message";
			}else{
				model.addAttribute("msg", "注册成功");
				return "/wx/user/success";
			}
		}catch(Exception e){
			return "/wx/user/register";
		}
	}
	
	@RequestMapping(value = "/saveUser",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public Results save(@RequestBody User user,RedirectAttributes attrs ) {
		ModelAndView mv = this.getModelAndView();
		Results results = null;
		
		User current = sysUserService.findUserByLoginName(user.getLoginName());
		if(current!=null){
			results = new Results("1", "注册账号已存在!");
			mv.setViewName("/wx/user/register");
		}else{
			user.setStatus(UserUtil.APPROVE_NO);
			user.setCreateTime(new Date());
			user.setPassword(MD5.md5(user.getPassword()));
			sysUserService.saveUser(user);
			mv.setViewName("/wx/user/approve");
			results = new Results("0", "注册成功,等待审批");
		}
		logger.info(new JacksonUtil().getJson(results));
		return results;
	}
	

	@RequestMapping(value = "/view")
	public String view(HttpServletRequest request,HttpServletResponse response,Model model) {
		OAuthInfo aouAuthInfo  = WeixinHelper.getUserOpenId(request);
		User user  = sysUserService.findUserByOpenId(aouAuthInfo.getOpenId(),false);
		model.addAttribute("user",user);
		return "/wx/user/viewinfo";
	}
	
	@RequestMapping(value = "/toApprove")
	public String toApprove(HttpServletRequest request,HttpServletResponse response,Model model) {
		return "/wx/user/approve";
	}
	
	@RequestMapping(value = "/listSection",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody 
	public String listSection(@RequestBody User user,HttpServletRequest request,HttpServletResponse response) {
		
		List<Section> list = sysSectionService.listSectionByPro(Integer.valueOf(user.getProjectID()));
		
		List<Item> items = new ArrayList<Item>();
		if(list!=null && list.size()>0){
			for(int i = 0;i<list.size();i++){
				Section section = list.get(i);
				Item item = new Item(section.getSecId()+"",section.getName());
				items.add(item);
			}
		}
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(),API_RETURN_STATUS.NORMAL.desc(),items);
		return new JacksonUtil().getJson(results);
	}
	
	
}
